Skip to content

Conversation

@bo-ram-bo-ram
Copy link
Member

@bo-ram-bo-ram bo-ram-bo-ram commented Feb 9, 2025

Pull request

Related issue

Resolve #81

Motivation and context

  • 내부 브로커를 제거하고 kafka를 통해 메세지를 연결할 수 있도록 합니다.
    • 채팅 메세지
    • 주식 data
  • 메세지를 메세지만 보냈을 때, 이미지를 첨부했을때, 동영상을 첨부했을 때 각각 맞는 응답 구조를 구현합니다.

Solution

  • kafka를 통한 data의 최종 흐름

    • 클라이언트가 STOMP를 통해 채팅 메시지를 /publish/chat.{channelId}로 전송
    • ChatController에서 KafkaProducer를 호출하여 Kafka로 메시지 전송 (jootalkpia.chat.prd.message)
      • channelId를 Key로 설정하여 같은 채팅방 메시지는 같은 파티션으로 전달
    • KafkaConsumer가 해당 메시지를 수신 (channelId 포함)
    • KafkaConsumer가 해당 메시지를 STOMP WebSocket을 통해 /subscribe/chat.{channelId}로 브로드캐스트
    • 채팅방에 참여한 모든 클라이언트가 해당 메시지를 실시간으로 수신
  • 인터페이스를 통해 메세지에 맞는 응답 구조를 구현했습니다.

    • MessageResponse 인터페이스를 구현하는 CommonResponse, TextResponse, VideoResponse, ImageResponse를 구현하였습니다.
    • 서비스단 기능별 메서드 분리 및 예외처리 추후 진행할 예정입니다.
    • 로컬 메모리와 유저 id 비교 로직 추후 구현 예정입니다.

How has this been tested

  • kafka를 통한 소켓 실시간 통신
    스크린샷 2025-02-10 오전 2 15 18

  • 첨부파일 형식 별 알맞은 응답
    스크린샷 2025-02-10 오전 3 53 26

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the docs/CONTRIBUTING.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@bo-ram-bo-ram bo-ram-bo-ram added ✨ Feature 기능 추가 🎮 BE 백엔드 ♻️ Need To Refactor 추후 리팩토링이 필요한 이슈 및 논의 무조건 스프린트내에 해야하는 것들 labels Feb 9, 2025
@bo-ram-bo-ram bo-ram-bo-ram self-assigned this Feb 9, 2025
@sgdevcamp2025 sgdevcamp2025 deleted a comment from netlify bot Feb 9, 2025
Copy link
Member

@mirlee0304 mirlee0304 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

것참 신기하군요 카프카란.. 수고하셨습니당!

Copy link
Collaborator

@ki-met-hoon ki-met-hoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!!👍

//스프링 인메모리 메시지 브로커 사용
config.enableSimpleBroker("/subscribe"); //구독 prefix
config.setApplicationDestinationPrefixes("/publish"); //발행 prefix
config.setApplicationDestinationPrefixes("/publish"); // STOMP 메시지 발행 prefix
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yml 파일 설정 추가 또는 @value를 쓰는 것도 방법이라고 생각합니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 부분은 대댓글 진행하며 prefix 추후 리팩토링 시 반영하겠습니다!

@Entity
@Getter
@Setter
public class Files {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entity가 하나라 BaseEntity를 생성 안하신걸까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞습니다! 근데 서버 내 이미 BaseTimeEntity를 구현했어서 수정하겠습니다! 꼼꼼리뷰..최고

public ChatMessageResponse createMessage(Long userId, String content){
User user = userRepository.findByUserId(userId);
return new ChatMessageResponse(user.getNickname(),content);
public List<MessageResponse> createMessage(ChatMessageRequest request) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수의 분리를 고민해보셔도 좋을 것 같아요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 부분은 추후 리팩토링 진행하겠습니다!

String stockDataJson = objectMapper.writeValueAsString(stockUpdate);

messagingTemplateBroker.convertAndSend("/subscribe/stock", stockDataJson);
messagingTemplate.convertAndSend("/subscribe/stock", stockDataJson);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String으로 변경할 필요가 있을까요? stockUpdate 객체를 보내도 JSON 변환이 안되나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인해보니 SimpMessagingTemplate내 convertAndSend 메서드 체인에서 doConvert 메서드가 호출되며, 이 과정에서 MessageConverter를 사용해 payload를 Message 객체로 변환되기도 한다네요!

하지만 저희같은 경우엔 ChatMessageToKafka내에 List로 되어있는데 MessageResponse가 인터페이스다보니 Jackson이 처리를 잘 못하는 경우가 발생합니다. 그래서 명시적으로 json 객체로 변환하는 것이 안전할 것 같습니다

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

명시적으로 Json 형태의 문자열로 만든다는 의미신거죠?
Spring에서 자체적으로 객체를 Json 데이터로 처리하는 것보다 Json 형태의 문자열로 변경하는게 더 안전할 것 같긴하네요!!


@KafkaListener(
topics = "${topic.chat}",
topics = "jootalkpia.chat.prd.message",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기는 yml 설정을 안가져온 이유가 있을까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

컨슈머 내 2개의 kafkaListener를 사용하니 인식이 잘 안되는 문제가 발생했습니다 ㅠㅠ 우선 변수로 수정 후 배포시에도 문제가 발생하면 수정해야겠어요

@bo-ram-bo-ram bo-ram-bo-ram merged commit f688fe5 into dev Feb 10, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🎮 BE 백엔드 ✨ Feature 기능 추가 ♻️ Need To Refactor 추후 리팩토링이 필요한 이슈 및 논의 무조건 스프린트내에 해야하는 것들

Projects

None yet

Development

Successfully merging this pull request may close these issues.

채팅서버 카프카 연결

4 participants